evaluate - evaluate algebraic strings

Overview

evaluate.c is a piece of documented example code showing an interesting and quick algorithim for evaluating algebraic strings, such as "2 + 4" and "a=5; b=10; c=b-a; b*c". It is released under the GNU General Public License version 2 or later.

Purpose

Mainly so I can write a new calculus.library for the Amiga, but to be honest, I'd like programmers who write evaluators to take a look at it and maybe learn something. I've used many evaluators, and often there are terrible mistakes. My pet peeves are:

Usage

Call this function: int evaluate(char *expr, struct val *result, struct vartable *vartable); Its arguments are:

Read about how the code works!

To compile the example eval.c, try one of these:

Expressions

In the demo program you can type in expressions and press return to have them evaluated. The rules regarding expressions are as follows:

You can follow any expression with a semicolon and another expression. The expression is a mixture of values, variables and operators, in infix notation. You can use parentheses (round brackets) to force a particular order. The operators are as follows:
+addition<<bitwise shift left
-subtraction (or negation)>>bitwise shift right
*multiplication&&logical and
/division||logical or
**raise to the power!logical not
%modulus==equality test
=assignment!=inequality test
&bitwise AND<less than test
|bitwise OR> greater than test
~bitwise NOT<=less than or equal to test
^bitwise XOR>=greater than or equal to test


The precedence of the operators is as follows, from highest (most tightly binding) to lowest:

The available functions are acos, asin, atan, cos, cosh, exp, ln, log, sin, sinh, sqr, sqrt, tan and tanh. All functions take one argument. You do not need to have parentheses round the argument.

Any text other than function is considered to be a variable reference. All variables have to be assigned before use (for example, "a=5; b=a-3; a+b"), or set as operating system environment variables.

The behaviour of operators and functions, and the accuracy of results is entirely down to the C compiler which compiles evaluate.c.
Kyzer/CSG